package com.school.controller;

import com.alibaba.druid.util.StringUtils;
import com.school.dto.score.GetScoreDTO;
import com.school.dto.score.InsertScoreDTO;
import com.school.model.ResultModel;
import com.school.model.Role;
import com.school.model.Score;
import com.school.service.RoleService;
import com.school.service.ScoreService;
import com.school.tool.Result;
import com.school.vo.score.ScoreAllRankVO;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;

@Controller()
@RequestMapping("/score")
public class ScoreController {
    @Autowired
    private ScoreService scoreService;

    @PostMapping()
    public @ResponseBody ResultModel insertRole(@Validated InsertScoreDTO dto, BindingResult result){
        if(result.hasErrors()){
            return Result.error(result.getFieldErrors().get(0).getDefaultMessage());
        }
        Score score=new Score();
        BeanUtils.copyProperties(dto,score);
        score.setIsDelete(1);
        score.setCreateTime(new Date());
        int resultCode=scoreService.insertSelective(score);
        if(resultCode>0){
            return Result.success();
        }else if(resultCode==-1){
            return Result.error("已打分，无法再次打分！");
        }else if(resultCode==-4){
            return Result.error("学生只能评论自己的任课教师!");
        }else if(resultCode==-2){
            return Result.error("教师编号或评论者编号错误！");
        }else if(resultCode==-3){
            return Result.error("学生与教师只允许评论自己系的教师！");
        }else{
            return Result.error("添加失败！");
        }
    }

    @GetMapping()
    public @ResponseBody ResultModel getRole(@Validated GetScoreDTO dto, BindingResult result){
        if(result.hasErrors()){
            return Result.error(result.getFieldErrors().get(0).getDefaultMessage());
        }
        Score score=scoreService.getByTIdAndDId(dto);
        return Result.success(score);
    }

    @GetMapping("/teacher")
    public @ResponseBody ResultModel getTeacher(@Param("tId") Integer tId){
        if(tId==null){
            return Result.error("参数错误");
        }
        ScoreAllRankVO score=scoreService.getScoreAndNumberByTId(tId);
        return Result.success(score);
    }

    @GetMapping("/roleRank")
    public @ResponseBody ResultModel getRoleDep(@Param("role") Integer role){
        if(role==null){
            return Result.error("参数错误");
        }
        ScoreAllRankVO score=scoreService.getScoreAndNumberByRole(role);
        return Result.success(score);
    }

    @GetMapping("/userNumberOne")
    public @ResponseBody ResultModel userNumberOne(@Param("depId") Integer depId){
        ScoreAllRankVO score=scoreService.userNumberOne(depId);
        return Result.success(score);
    }


    @GetMapping("/myToTeacher")
    public @ResponseBody ResultModel myToTeacher(@Param("cId") Integer cId,@Param("tId") Integer tId){
        if(tId==null||cId==null){
            return Result.error("参数错误");
        }
        Score score=scoreService.getMyToTeacher(cId,tId);
        return Result.success(score);
    }

    @GetMapping("/param")
    public @ResponseBody ResultModel myToTeacher(@Param("tId") Integer tId){
        List<Score> score=scoreService.getArray(tId);
        return Result.success(score);
    }

}
